home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / prog / sgf2mi13.taz / sgf2mi13 / options.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-10  |  7.5 KB  |  313 lines

  1. /* #[info:        */
  2. /************************************************************************
  3. *                                    *
  4. *              Jan van der Steen                *
  5. *                                    *
  6. *          Centre for Mathematics and Computer Science        *
  7. *              Amsterdam, the Netherlands            *
  8. *                                    *
  9. *-----------------------------------------------------------------------*
  10. * File    : options.c                            *
  11. * Purpose : Get the command line options                *
  12. * Version : 1.10                            *
  13. * Modified: 3/10/93 13:36:40                        *
  14. * Author  : Jan van der Steen (jansteen@cwi.nl)                *
  15. *-----------------------------------------------------------------------*
  16. ************************************************************************/
  17. /* #]info:        */ 
  18. /* #[include:        */
  19.  
  20. #include <stdio.h>
  21. #ifdef __STDC__
  22. #  include <stdlib.h>
  23. #endif
  24.  
  25. #include <string.h>
  26. #include "game2eps.h"
  27. #include "game2tex.h"
  28. #include "tools.h"
  29. #include "patch.h"
  30.  
  31. /* #]include:        */ 
  32. /* #[defines:        */
  33.  
  34. #define    ARGCH    (int)':'
  35. #define BADCH     (int)'?'
  36. #define EMSG     ""
  37. #define tell(s)    fputs(*nargv,stderr);    \
  38.         fputs(s,stderr);    \
  39.         fputc(optopt,stderr);    \
  40.         fputc('\n',stderr);    \
  41.         return(BADCH);
  42.  
  43. /* #]defines:        */ 
  44. /* #[static:        */
  45.  
  46. static    int    optopt;        /* character checked for validity    */
  47. static    char *    optarg;        /* option counter            */
  48.  
  49. /* #]static:        */ 
  50. /* #[extern:        */
  51.  
  52. extern    EPSFINFO epsf;        /* The eps filename/directory        */
  53. extern    char *     program;    /* Program name                */
  54. extern    int     prn_dev;    /* DEVICE_{EPS,TEX}            */
  55. extern    int     verbose;    /* Verbose mode                */
  56. extern    int     caching;    /* Caching mode                */
  57. extern    int     textout;    /* Emit the text too            */
  58. extern    int     statout;    /* Emit the kibitz stats        */
  59. extern    int     coords;    /* Print coordinates            */
  60. extern    int     optind;    /* index into argv vector        */
  61. extern    char *     optarg;    /* option pointer            */
  62. extern    int     figlist[];    /* Figure  list                */
  63. extern    int     dialist[];    /* Diagram list                */
  64. extern    int     user_figs;    /* Users fig list?            */
  65. extern    int     user_dias;    /* Users dia list?            */
  66.  
  67. /* #]global:        */
  68.  
  69. /* #]extern:        */ 
  70. /* #[prototype:        */
  71.  
  72. #ifdef __STDC__
  73. #    define  PROTO(s) s
  74. #else
  75. #    define  PROTO(s) ()
  76. #endif
  77.  
  78. static    void    usage        PROTO((int exitcode));
  79. static   int    getopt        PROTO((int nargc, char **nargv, char *ostr));
  80. static    void    getlist        PROTO((char *userlist, int mylist[]));
  81.  
  82. #undef PROTO
  83.  
  84. /* #]prototype:        */ 
  85.  
  86. /* #[getopt:        */
  87.  
  88. static int
  89. getopt(nargc,nargv,ostr)
  90. int       nargc;
  91. char    ** nargv;
  92. char    *  ostr;
  93. {
  94.     static char    *place = EMSG;        /* option letter processing */
  95.     char    *oli;            /* option letter list index */
  96.  
  97.     if (*place == 0) {            /* update scanning pointer */
  98.     if (optind >= nargc            ||
  99.         *(place = nargv[optind]) != '-'    ||
  100.         ! *++place) return(EOF);
  101.     if (*place == '-') {        /* found "--" */
  102.         ++optind;
  103.         return(EOF);
  104.     }
  105.     }                    /* option letter okay? */
  106.     if ((optopt = (int)*place++) == ARGCH ||
  107.     (oli = strchr(ostr, optopt)) == 0)   {
  108.     if (*place == 0) ++optind;
  109.     tell(": illegal option -- ");
  110.     }
  111.     if (*++oli != ARGCH) {        /* don't need argument */
  112.     optarg = NULL;
  113.     if (*place == 0) ++optind;
  114.     }
  115.     else {                /* need an argument */
  116.     if (*place) optarg = place;    /* no white space */
  117.     else if (nargc <= ++optind) {    /* no arg */
  118.         place = EMSG;
  119.         tell(": option requires an argument -- ");
  120.     }
  121.      else optarg = nargv[optind];    /* white space */
  122.     place = EMSG;
  123.     ++optind;
  124.     }
  125.     return(optopt);            /* dump back option letter */
  126. }
  127.  
  128. /* #]getopt:        */ 
  129. /* #[getoptions:    */
  130.  
  131. #define OPTIONS "D:f:S:PTCchkqstv"
  132.  
  133. void
  134. getoptions(argc, argv)
  135. int argc;
  136. char **argv;
  137. {
  138.     int      c;
  139.     char *s;
  140. #ifndef __STDC__
  141.     extern char *getenv();
  142. #endif
  143.  
  144. #if __unix || unix || UNIX
  145.     epsf.eps_dir = "/tmp";
  146. #else
  147.     epsf.eps_dir = "k:\\tmp";
  148. #endif
  149.     epsf.eps_size = 180;
  150.     if ((s = getenv("EPSFTMP" )) != (char *) 0) epsf.eps_dir  = s;
  151.     if ((s = getenv("EPSFSIZE")) != (char *) 0) epsf.eps_size = atoi(s);
  152.  
  153.     while ((c = getopt(argc, argv, OPTIONS)) != EOF)
  154.     switch (c) {
  155.             /* EPS directory */
  156.     case 'D':    epsf.eps_dir = optarg;            break;
  157.  
  158.             /* Emit EPS diagrams */
  159.     case 'P':    prn_dev = DEVICE_EPS;            break;
  160.  
  161.             /* Emit TeX diagrams */
  162.     case 'T':    prn_dev = DEVICE_TEX;            break;
  163.  
  164.             /* Enable caching */
  165.     case 'C':    caching = 1;                break;
  166.  
  167.             /* Set diagram size */
  168.     case 'S':    epsf.eps_size = atoi(optarg);        break;
  169.  
  170.     case 'f':    /* Get user list of figures */
  171.             getlist(optarg, figlist);
  172.             user_figs = 1;                break;
  173.  
  174.     case 'd':    /* Get user list of diagrams */
  175.             getlist(optarg, dialist);
  176.             user_dias = 1;                break;
  177.  
  178.             /* Disable coordinates */
  179.     case 'c':    coords = 0;                break;
  180.  
  181.             /* Disable statistics output */
  182.     case 's':    statout = 1;                break;
  183.  
  184.             /* Dispose text */
  185.     case 't':    textout = 0;                break;
  186.  
  187.             /* Set message level */
  188.     case 'v':    verbose = 1;                break;
  189.  
  190.             /* Show program version and exit(0) */
  191.     case 'q':    fprintf(stdout, "%s-%d.%d\n", program,
  192.                 RELEASE, PATCHLEVEL);
  193.                                 exit(0);
  194.  
  195.     case 'h':    usage(0);    /* Online help    */
  196.     case '?':
  197.     default :    usage(1);    /* Error    */
  198.     }
  199. }
  200.  
  201. /* #]getoptions:    */ 
  202. /* #[usage:        */
  203.  
  204. #define FMTS2(s1,s2)    fprintf(stderr, "%-16s%-40s\n", s1, s2)
  205. #define FMTS3(s1,s2,s3)    fprintf(stderr, "%-16s%-40s%s\n", s1, s2, s3)
  206. #define FMTN3(s1,s2,n1)    fprintf(stderr, "%-16s%-40s%d\n", s1, s2, n1)
  207. #define DOTTEDLINE    {\
  208.                 int i;\
  209.                 for (i = 0; i < 70; i++) fputc('-', stderr);\
  210.                 fputc('\n', stderr);\
  211.             }
  212.  
  213. static void
  214. usage(code)
  215. int code;
  216. {
  217.     fprintf(stderr    , "Usage: %s [options] [file]\n", program);
  218.     DOTTEDLINE
  219.     FMTS3("Option"    , "Description"            , "Default");
  220.     DOTTEDLINE
  221.     FMTS3("[-D path]"    , "EPS directory"        , epsf.eps_dir);
  222.     FMTS3("[-P]"    , "Emit PS  for diagrams"    , "on");
  223.     FMTS3("[-T]"    , "Emit TeX for diagrams"    , "off");
  224.     FMTS3("[-C]"    , "Caching mode"        , "off");
  225.     FMTN3("[-S size]"    , "Diagram size [72 = 1in]"    , epsf.eps_size);
  226.     FMTS3("[-d list]"    , "Emit diagrams in list"    , "off");
  227.     FMTS3("[-f list]"    , "Emit figures  in list"    , "when text");
  228.     FMTS3("[-s]"    , "Emit kibitz statistics"    , "off");
  229.     FMTS3("[-c]"    , "Don't print coordinates"    , "on");
  230.     FMTS3("[-t]"    , "Don't print text output"    , "on");
  231.     FMTS3("[-v]"    , "Verbose mode"        , "off");
  232.     FMTS2("[-q]"    , "Print program version");
  233.     FMTS2("[-h]"    , "Print usage");
  234.  
  235.     exit(code);
  236. }
  237.  
  238. /* #]usage:        */ 
  239.  
  240. /* #[getlist:        */
  241.  
  242. static void
  243. getlist(userlist, mylist)
  244. /*
  245.  * Get the list of figures to print
  246.  *
  247.  * List can be any of:
  248.  *
  249.  *    %n        Create figure after each n moves
  250.  *    k+l+...+n    Create figure at move k, l, ..., n
  251.  *
  252.  * We fill the mylist array with the move/diagram numbers where we
  253.  * will print a figure/diagram
  254.  */
  255. char *userlist;
  256. int   mylist[];
  257. {
  258.     int interval;
  259.     int number;
  260.     int n = 0;
  261.  
  262.     /*
  263.      * First we fill the array with numbers != 0
  264.      */
  265.     for (n = 0; n < MAXNUM; n++) figlist[n] = 999; n = 0;
  266.     if (*userlist == '%') {
  267.     if (mylist == dialist) {
  268.         fprintf(stderr,
  269.             "%s: This format is only allowed for figures\n",
  270.             program);
  271.         exit(1);
  272.     }
  273.     /*
  274.      * Interval request
  275.      */
  276.     interval = atoi(userlist+1);
  277.     if (interval < 1 || interval >= MAXNUM) {
  278.         fprintf(stderr,
  279.             "%s: Figure interval out of range (1 - %d)\n",
  280.             program, MAXNUM);
  281.         exit(1);
  282.     }
  283.     while ((number = (n+1)*interval) < MAXNUM) mylist[n++] = number;
  284.     }
  285.     else {
  286.     /*
  287.      * User list request
  288.      */
  289.     char *delim = "+,-", *t;
  290.  
  291.     t = strtok(userlist, delim);
  292.     while (t != NULL) {
  293.         number = atoi(t);
  294.         if (number >= MAXNUM) {
  295.         if (mylist == figlist)
  296.             fprintf(stderr,
  297.             "%s: Move number %d out of range (< %d)\n",
  298.             program, number, MAXNUM);
  299.         else
  300.             fprintf(stderr,
  301.             "%s: Diagram number %d out of range (< %d)\n",
  302.             program, number, MAXNUM);
  303.         exit(1);
  304.         }
  305.         mylist[n++] = number;
  306.         t = strtok(NULL, delim);
  307.     }
  308.     }
  309. }
  310.  
  311. /* #]getlist:        */ 
  312.  
  313.